Okay, so today we are going to start looking at if statements.
This will be split up into two lessons because there are quite a large topic.
But believe me, they are very useful for you to learn.
Okay, so add a script.
I'm going to put it into server script service this time, just change things around a bit and create
two parts.
Call one of these part one and the other one part two.
Okay, so we've got two parts here.
Um, then I'm going to create two variables for these parts okay.
So local part one is equal to game dot workspace dot part one.
And then local part two is equal to game dot workspace dot part two.
Okay.
Um most scripts starting off in our lessons now are going to be making some variables for us to use
later on.
Okay.
Um, but yeah, so if statements, what they are is um, they allow you to run code, but only if something
happens or something is met.
Okay.
So currently the one when we write code in a script, it, it runs the code.
It does what I wanted it to do no matter what happens.
Okay.
But sometimes you don't always want that to happen.
Um, for example, if you have a, um, if you, if you, if players are running a race and you want
to maybe, maybe play some confetti on the screen and the player who won, okay.
Um, you can't write, um, you wouldn't be able to write out, um, a script which will display confetti.
Um, because what would happen is if you did that, it wouldn't be on local script.
But if you did that, it would happen for everybody.
So you'd have to check to see who won the race.
And if this player did win the way it did, did win win the race?
Um, then play the confetti on their screen.
So this example about how sometimes you only want things to happen based if based on if something happens
in your game.
Okay.
And which is why if statements are so useful, okay.
As they run code dependent on a condition, and the condition is what controls, um, if the code will
run okay.
If this condition is met, the code will run.
Otherwise the code will not run.
Okay.
Um, so if statements we start off by using the keyword if, okay, that's the keyword we use to start
off writing our if statement.
Okay.
Then we write our condition.
So this is what controls whether the code we're going to write below happens or not okay.
So what I'd like to do is I've got two parts here.
What I'm going to do is if one of these parts, um, have a have, have the really red color, then
we're going to make the other one have a really red color.
Okay.
Um, so let's just write that out.
So if part one.
So we're just going to reference, um, the brick color property okay.
Part one dot brick color is equal to brick color dot new and put in here really red okay.
So this here is us writing out the property okay.
Whereas in this whereas this time we're not making the brick color property equal to really red, we're
just checking if it's equal to really red okay.
So when you're actually wanting to change the property it's a statement.
Okay.
Um, but in this case is a condition.
So you're checking it.
You're not.
We're not saying brick color is equal to this.
We're kind of asking it does it equal to this, which is kind of the reason why we include two equal
signs instead of just one.
Okay.
Bit confusing I know.
But the main reason is because it's a condition, not a statement.
Um, so it's a very important to remember two equal signs when you are checking for something.
Okay.
Um, so once you've written out your condition, make sure you use two equal signs, but everything
else is the exact same.
Um, then you can just press enter and it will automatically, automatically sort out the rest of it
for you.
Okay.
At the end of the first line, it will, it will insert a then keyword and this then keyword.
The only purpose of it really is just to, um, I don't know, really doesn't have too much of a purpose,
but it kind of signifies that.
I guess you finished writing the condition.
Um, but the.
But the end at the bottom of it.
Here.
Um, what this end means is anything written before the end will be dependent on this condition.
So this condition controls if any code written before, before we end works.
But any code written after the end is not inside the if loop.
So it will not be controlled by whether the brick color is really red or not.
Okay.
So that's really useful for us.
So um, so we want to figure out, um, so this will basically basically figure out if the brick color
is equal to the really red, the really red name.
Okay.
Um, if that is the case, then the code I write in here.
So whatever code I write in this little bit here will will run.
Okay.
So I wanted the it it's also change the color of the second part.
So I'm just going to write that in here I'm going to say part two dot brick color is equal to any one
equals sign.
This time because we're actually changing the property.
Not checking it is equal to brick color dot new.
And then writing really red again.
Okay.
Um, and that's it.
Okay.
So this bit here is checking if the first part has the really red color.
Okay.
If this here is true.
So if this here is met then it will run the code here.
If this is not met it will just carry on with the rest of the code.
It won't it will ignore this and it will just carry on with the rest of my code.
Okay, so let's try this out.
So if I quickly run the game.
It will change both of them to be really red.
But if I were to change this to say yellow, for example, of a new yellow color, it will not change
it to really red.
Okay, so that's a really cool one.
Just an example on how if statements are so useful.
Okay.
Um, they can we can make these a bit more advanced though.
Okay.
Um, so um, currently we are just checking for one property or one condition, but you can have multiple
conditions.
Okay.
So if you want to add a second condition.
All right.
You can write your first one.
Put a space.
Then you either write and or you can write or okay.
The difference between the two is if you write and both of your two conditions must be true for the
code to run.
But if you write or only one of those two conditions have to be met in order for the code to run.
Okay, we're going to write.
And in this case, and I want it to also be visible, um, for this to work.
So in order for it to be visible, the transparency, um, has to be, well, we can say with transparency
zero.
So if it's, if it's even somewhat transparent, we don't want this to work.
Okay.
So let's say here and part one dot transparency is equal equal zero okay.
And that's it.
That's when two conditions checking both if it's really red and if it's completely visible.
If it is then it will change the second part's color.
And we can also maybe say as well part two dot transparency is equal to zero as well, just in case
it's not okay.
And that's it for the form of if loops.
Okay.
Um, some symbols that you should be aware of.
Okay.
As we looked at the two equal signs.
But you can get a bit more advanced than that.
Okay.
For example, with a transparency, this will check if the part is completely transparent or sorry.
It will check if the part is not transparent at all.
Even if it's 0.01 on the transparency scale, it's still incredibly invisible, but there's a tiny amount
that you can see through.
It will not work, which isn't really doesn't really make too much sense because it's still absolutely
visible.
Okay.
Um, the only time it's invisible is when transparency is 10.999.
Transparency is still somewhat visible.
Okay.
So we want to find out some way to instead of saying if it's equal to zero saying instead if it's not
equal to one.
Okay.
Um, so let's have a look at some of these signs.
So we know that two equal signs means equal to.
Okay.
However you can use this symbol here.
And an equal sign which means not equal to.
Okay.
So in this case over here we could say is not equal to one.
And then that would instead check but is not one okay.
Which includes more um, includes more values of transparency which is very good.
Okay.
Some other symbols that you need to be aware of.
You can also use the greater than and less than signs.
So this one here is greater than.
And this one here is less than.
So for example, for the transparency, again you could say if a transparency is lower than 0.5 because
0.5 is kind of halfway, so you want it quite visible.
Um, but maybe when it becomes a bit too bit, a bit less visible for it not to work.
Okay.
Um, you can also pair up the greater than and equal to signs to make greater than.
Or equal to.
Okay.
So this will work if, if I wrote it here for example.
So this would work if the past transparency here is bigger than one or is equal to one.
Okay.
Just as an example.
And the same thing applies for the less than you have less than or equal to.
Okay.
Um, one more as well is um, not now not just works for boolean values.
So let's add maybe a third one in here and say and let's check it's transparent.
It's anchored.
Sorry.
Um, so part one dot anchored um is equal.
Equal true.
Okay.
Um, so this one's a bit this one's a bit funny.
So you can write it like this and that will work completely fine.
However, writing this and just writing this here actually mean the same thing.
The reason why is because this here is basically saying is anchored equal to true.
Okay.
This here can either be correct or it can be incorrect.
So this this condition here can either be true or false.
Okay.
So if this condition has the value of true it will work.
Which is the same as writing if anchored is equal to true.
Okay, so with booleans, if you want it to be true in order to work, you can get rid of the is equal
to equal to true bit because it means the same thing.
And if you want it to be false, you can just write not at the front there and not part the bundle anchored
and not means the opposite.
So the opposite of true.
So not true which means false.
Okay, so quite a bit to remember with if statements.
But this is the first lesson.
So we will look at more more of these examples in next lesson when we go into a bit more detail about
if if statements.
So try some of these out.
They are really useful.
Um and they're not, they're not, they're not easy but they are very useful.
Um, and then join me in the next lesson and we'll continue on looking at if statements.